#!/bin/bash
#
# dart2:       Manage a dart2 server
#
# Author:      Eric Boix ?
#
# Description: 
#
# References: http://na-mic.org/Wiki/index.php/Dart2Summary
#
# Changelog : 
#             EQ 2007-03-26 :
#             - correct several information mentions
#             - change "export LANG" to "export LANG=C" as on test host
#             - correct shutdown of server (use DARTClient and -q option)
#             - transition from 1.0.0 to 1.0.7 DART version
#             - change the launches : dart user is the owner of dart processes 
#             EC 2007-03-22 : 
#             - add "export LANG=US" to workaround bug (proposal EQ)


##### Configuration section:
JAVACOMMAND=/usr/bin/java

export LANG=C

# Name of user running Dart
USER="dart"

# Where to find Dart2 installation directory i.e. basically the absolute
# path to DartServer.jar file
#INSTALLDIR=/home/dart/local/Dart1.0.0/
INSTALLDIR=/home/dart/local/Dart1.0.7/

# The absolute filename of the jar of dart server part:
DARTSERVER=$INSTALLDIR/DartServer.jar

# The absolute filename of the jar of dart client part:
DARTCLIENT=$INSTALLDIR/DartClient.jar

# The path to the configuration of the dart server i.e. the directory
# generated with the "java -jar DartServer.jar --createserver" command:
SERVERCONF=$INSTALLDIR/DIETServer

# The path to the configuration of the project i.e. the directory
# generated with the "java -jar DartServer.jar --create" command:
PROJECTCONF=$INSTALLDIR/DIETcore

# Log file of dart:
DARTLOG=$INSTALLDIR/Dart-$(date "+Y%m%d%H%M").log


##### End of configuration section:

if [ ! -d $INSTALLDIR ] ; then
  echo "Installation directory does not exist."
  exit 0
fi

if [ ! -f $DARTSERVER ] ; then
  echo -n $"DartServer.jar not found. [ Was expecting "
  echo -n $DARTSERVER
  echo    "]"
  exit 0
fi

if [ ! -f $JAVACOMMAND ] ; then
  echo -n $JAVACOMMAND
  echo -n $"expected java command not found."
  exit 0
fi

start() {
  echo $"Starting Dart2 server."
  su $USER -c "$JAVACOMMAND -jar $DARTSERVER $SERVERCONF $PROJECTCONF > $DARTLOG & "
}

stop() {
  echo $"Halting Dart2 server."
  # EQ : yes, it's strange but DARTClient is used to shutdown DARTServer...
  su $USER -c "$JAVACOMMAND -jar $DARTCLIENT -q $PROJECTCONF >> $DARTLOG 2>&1"
}

status() {
  if [ ! -f $DARTCLIENT ] ; then
    echo -n $"DartClient.jar not found. [ Was expecting "
    echo -n $DARTCLIENT
    echo    "]"
    exit 0
  fi
  echo -n $"Dart status of project "
  echo    $PROJECTCONF
  su $USER -c "$JAVACOMMAND -jar $DARTCLIENT --getstatus $PROJECTCONF"
}



# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        ;;
  restart|reload)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload}"
        exit 1
esac

exit 0
